#include <iostream>
#include <log4cxx/logger.h>
#include <log4cxx/xml/domconfigurator.h>

#include <sys/types.h>

#include "MysqlConnPool.h"


using std::cout;
using std::endl;

MysqlConnPool g_pool;

void * thread_routine(void * arg) {
	mysql_thread_init();
	pthread_t tid = pthread_self();
	while (true) {
		MysqlConnPtr ptr;
		int ret = g_pool.get(ptr);
		if (ret < 0) {
			cout << "cound not get mysql connection" << endl;
			sleep(1);
			continue;
		}
		string sql = "select User from user";
		ret = mysql_real_query(ptr.get(), sql.c_str(), sql.size());
		MYSQL_RES  * mysql_res = mysql_store_result(ptr.get());
		MYSQL_ROW row;
		while (NULL != (row = mysql_fetch_row(mysql_res))) {
			cout << "tid:" << tid << ", user:" << row[0] << endl;
		}
		mysql_free_result(mysql_res);
		sleep(1);
	}
}	

int test() {
	xml::DOMConfigurator::configure("./log.xml");
	g_pool.init("127.0.0.1", 3306, "root", "hermes2good", "mysql", 10);
	pthread_t t[10];
	for (int i =0; i < 30; i++) {
		pthread_create(&t[i], NULL, thread_routine, NULL);
	}
	for (int i = 0; i < 30; i++) {
		pthread_join(t[i],  NULL);
	}
	return 1;
}

int main(int argc, char * argv[]) {
	test();
}
